const net = require('net');
const tls = require('tls');
const aes = require('../lib/aes');
const md5 = require('../lib/md5');
const uuid = require('../lib/uuid');
// const key = '47e43adc6f31eae15b359186fb198256e41e749b38a1963e7e9ba20551a7bf3aa9ce4e69de7e36de17bd9695e88eea3117e1aae02122fa846ee29722ed17e395ea9d82ed8117bac775b11afaaaa7cf3746be24ef658db23e8e8123b8e7e1da863f997aa2ee8e71fad94743ea9ee731a5ebec86becb49e7b6eae6e7';//1ce7213246
// const iv = '42aaeda0a68e724337eefd7658939ed4ea048afe96319f3628985ae74b7eee52a89a9ebce5a7ee86c62a612bfeb5bc9ed6465bec18eba472ddefdebb1ae163c2a496efb1beee1b14933ee197f2fe1e8b81d76e121273d1eb79e75b5847523377eeaa1177713ae4a1da189198a51aa4ca7e6c92862eee9e716a737f';//1e33ae8392
const key = 'key';
const iv = 'iv';
console.time('connect');
let conn = net.connect({
  host: 'iy88.site',
  port: 1223
}, () => {
  console.timeEnd('connect');
  conn.on('end', () => {
    console.log('end');
    process.exit(-1)
  })
  conn.on('close', () => {
    console.log('close');
    process.exit(-1)
  })
  conn.on('error', () => {
    console.log('error');
    process.exit(-1);
  })
  console.time('auth');
  conn.write(JSON.stringify({ t: 'auth', d: 'ljx', cid: uuid(), f: 'node' }));
  conn.on('data', buffer => {
    let raw = buffer.toString();
    if (raw) {
      let data = JSON.parse(buffer.toString());
      if (data.t === 'auth') {
        // salt = data.s;
        // c = aes(key + salt, iv + salt);
        console.timeEnd('auth');
      } else if (data.t === 'get') {
        console.time('send res');
        let req = data.req;
        let host = req.h;
        let port = req.p;
        let ssl = req.s;
        let context = req.c;
        if (ssl) {
          let tlsConn = tls.connect({ host, port }, () => {
            tlsConn.write(context);
            tlsConn.on('data', buffer => {
              let arr = [];
              for (let i = 0; i < buffer.length; i++) {
                arr.push(buffer[i]);
              }
              let res = arr.join('|');
              let max = 1300;
              if (res.length > max) {
                let slices = [];
                let n = Math.floor(res.length / max);
                for (let i = 0; i < n + 1; i++) {
                  let now = i * max;
                  slices.push(res.slice(now, now + max));
                }
                for (let i = 0; i < slices.length; i++) {
                  let sb = { t: 'node_res', cid: data.cid, ret: 1, slice: true, end: false, data: { data: slices[i] } };
                  if (i === slices.length-1) {
                    sb.end = true;
                  }
                  conn.write(JSON.stringify(sb));
                  console.log(sb);
                }
              } else {
                conn.write(JSON.stringify({ t: 'node_res', cid: data.cid, ret: 1, data: res }));
              }
              console.timeEnd('send res');
              tlsConn.destroy();
            })
          })
        } else {
          let tcpConn = net.connect({ host, port }, () => {
            tcpConn.write(context);
            tcpConn.on('data', buffer => {
              let arr = [];
              for (let i = 0; i < buffer.length; i++) {
                arr.push(buffer[i]);
              }
              let res = arr.join('|');
              conn.write(JSON.stringify({ t: 'node_res', cid: data.cid, ret: 1, data: 'tttttt' }));
              console.timeEnd('send res');
              console.timeEnd('send res');
              tcpConn.destroy();
            })
          })
        }
      }
    } else {
      console.log('self-end');
      conn.end();
    }
  })
})